<template>
	<view class="cell-page">
		<view class="search-box pt-0 pb-0">
			<u-search placeholder="搜索" shape="square" v-model="searchData.name" @search="onSearch" :showAction="false"/>
			<view class="custom-btn" @click.stop="$u.route('/pages/customer/information')" v-if="userInfo.roleCategoryId!==2">新增客户</view>
		</view>
		<view class="bg-white" v-if="userInfo.roleCategoryId===2">
			<u-tabs 
				:list="tabsList" 
				:activeStyle="{color:'#e54d42'}" 
				lineColor="#e54d42" 
				:scrollable="false" 
				:current="currentTabs"
				lineWidth="50"
				@click="onClickSwitchType"
			/>
		</view>
		<template v-if="userInfo.roleCategoryId!==2">
			<view class="bg-white padding-20 pb-0">
				<u-row justify="space-between" gutter="5">
					<u-col span="6">
						<u-button
						text="即将生日"
						size="large"
						icon="/static/images/by.png"
						color="#CD2626"
						@click="$u.route('/pages/customer/UserBirthday/index')">
						</u-button>
					</u-col>
					<u-col span="6">
						<u-button
						text="久未到店"
						size="large"
						icon="/static/images/wdd.png"
						color="#666699"
						@click="$u.route('/pages/customer/SleepUser/index')">
						</u-button>
					</u-col>
					<!-- <u-col span="4">
						<u-tag 
							text="未绑定门店" 
							bgColor="#990099" 
							borderColor="#990099" 
							icon="/static/images/gl.png" 
							size="large" 
							@click="$u.route('/pages/customer/unbound/index/index')">
						</u-tag>
					</u-col> -->
				</u-row>
			</view>
			<view class="flex justify-between bg-white padding-20 mb-10">
				<view class="">
					<text>全部客户</text>
					<text class="fsize-24 line-gray ml-10">{{total}}位</text>
				</view>
				<view class="flex align-center" @click="showPopup = true">
					<text class="fsize-28">筛选</text>
					<uni-icons custom-prefix="iconfont" type="icon-shaixuan" size="17" />
				</view>
			</view>
		</template>
		<view class="my-list padding-0">
			<!-- :height="swiperHeight" -->
			<u-list>
				<u-list-item v-for="(item, index) in dataList" :key="item.id">
					<view 
						class="flex align-center bg-white pl-20 pr-20" 
						@click="$u.route('/pages/customer/details',{id: item.id})"
						v-if="userInfo.roleCategoryId!==2"
					>
						<view class="shrink0">
							<u-avatar 
								:src="item.avatarImage" 
								size="46" 
								default-url="/static/logo.png"/>
						</view>
						<view class="flex justify-between align-center w-100 border-b pl-10 ml-14 pt-24 pb-24">
							<view class="">
								<view class="flex">
									<text>{{item.nickName}}</text>
									<u-icon v-if="item.sex!==3" size="15" :name="item.sex===1?'man':'woman'" :color="item.sex===1?'#3c9cff':'#f56c6c'" />
								</view>
								<view class="fsize-26 line-gray mt-10">{{item.gradeName || '--'}}</view>
								<view class="fsize-24 line-gray mt-10">{{item.storeName}}</view>
							</view>
							<!-- @click.stop="phoneCall(item.mobile)" -->
							<view @click.stop>
								<!-- <uni-icons custom-prefix="iconfont" type="icon-phone" color="#81c66d" size="30"/> -->
								<u-tooltip :text="item.mobile" overlay></u-tooltip>
								<view class="fsize-24 line-gray mt-10">推荐人: {{item.staffName}}</view>
							</view>
						</view>
					</view>
					<view class="pl-20 pr-20 mt-20 fsize-28" v-else>
						<view class="bg-white pl-20 pr-20 border-radius-10">
							<view class="flex align-center border-b pb-20 pt-20">
								<view class="shrink0">
									<u-avatar 
										:src="item.avatarImage" 
										size="40" 
										default-url="/static/logo.png"/>
								</view>
								<view class="flex justify-between align-center w-100 pl-10 ml-14">
									<view class="">
										<view class="flex">
											<text class="fweigh-600">{{item.nickName}}</text>
											<u-icon v-if="item.sex!==3" size="15" :name="item.sex===1?'man':'woman'" :color="item.sex===1?'#3c9cff':'#f56c6c'" />
										</view>
										<view class="line-gray mt-10">{{item.gradeName}}</view>
									</view>
									<!-- @click.stop="phoneCall(item.mobile)" -->
									<view @click.stop>
										<!-- <uni-icons custom-prefix="iconfont" type="icon-phone" color="#81c66d" size="30"/> -->
										<u-tooltip :text="item.mobile" overlay></u-tooltip>
									</view>
								</view>
							</view>
							<view class="flex justify-between align-center fsize-28 line-gray pt-20 pb-20">
								<view class="">
									<text>帮推</text>
									<text class="line-cyan">0</text>
									<text>人 · 共消费</text>
									<text class="line-red">￥0</text>
								</view>
								<text>{{item.createTime}}</text>
							</view>
						</view>
					</view>
				</u-list-item>
			</u-list>
			<u-empty text="内容为空" mode="data" v-if="dataList.length===0" marginTop="150" />
		</view>
		<u-popup
			:closeOnClickOverlay="true"
			:show="showPopup"
			mode="right"
			bgColor="#f8f8f8"
			@close="showPopup=false"
		>				
			<view class="width-580 padding-20">
				<view class="">
					<text class="fweigh-600">上次到店时间</text>
					<view class="flex flex--wrap mt-20">
						<view class="mr-20" v-for="(ti, tx) in arriveDays" :key="tx">
							<u-tag 
								:text="ti.label" 
								:name="ti.value" 
								:bgColor="timesIx===tx?'#e54d42':'#fff'" 
								:borderColor="timesIx===tx?'#e54d42':'#9a9a9a'" 
								:color="timesIx===tx?'#fff':'#353535'" 
								:plain="timesIx!==tx" 
								shape="circle" 
								size="large"
								@click="setData('arriveDays', ti.value, tx)"
							/>
						</view>
						<view class="mt-20 width-246">
							<u-input 
								ref="timeInput"
								:value="searchData.arriveDays" 
								type="number" 
								placeholder="自定义时间" 
								shape="circle" 
								inputAlign="center"
								:customStyle="inputStyle"
								fontSize="13px"
								@change="onSetTime"
							>
								<template slot="suffix">
									<text class="fsize-26">天前</text>
								</template>
							</u-input>
						</view>
					</view>
				</view>
				<view class="mt-20">
					<text class="fweigh-600">消费次数</text>
					<view class="flex flex--wrap mt-20">
						<view class="mr-20" v-for="(consume, consumeIx) in consumeTimes" :key="consume.value">
							<u-tag 
								:text="consume.label" 
								:name="consume.value" 
								:bgColor="consumeIndex===consumeIx?'#e54d42':'#fff'" 
								:borderColor="consumeIndex===consumeIx?'#e54d42':'#9a9a9a'" 
								:color="consumeIndex===consumeIx?'#fff':'#353535'" 
								:plain="consumeIndex!==consumeIx" 
								shape="circle" 
								size="large"
								@click="setData('consumeTime', consume.value, consumeIx)"
							/>
						</view>
						<view class="mt-20 flex align-center">
							<u-input 
								ref="timeInput"
								:value="searchData.minCconsumeTime" 
								type="number" 
								placeholder="最低次数" 
								shape="circle" 
								inputAlign="center"
								:customStyle="inputStyle"
								fontSize="13px"
								@change="consumeIndex=100"
							>
							</u-input>
							<text class="ml-10 mr-10">—</text>
							<u-input
								ref="timeInput"
								:value="searchData.maxCconsumeTime" 
								type="number" 
								placeholder="最高次数" 
								shape="circle" 
								inputAlign="center"
								:customStyle="inputStyle"
								fontSize="13px"
								@change="consumeIndex=100"
							>
							</u-input>
						</view>
						<view class="mt-20">
							<text class="fweigh-600">消费金额</text>
							<view class="mt-20 flex align-center">
								<u-input 
									ref="timeInput"
									:value="searchData.minConsumeAmount" 
									type="number" 
									placeholder="最低金额" 
									shape="circle" 
									inputAlign="center"
									:customStyle="inputStyle"
									fontSize="13px"
								>
								</u-input>
								<text class="ml-10 mr-10">—</text>
								<u-input
									ref="timeInput"
									:value="searchData.maxConsumeAmount" 
									type="number" 
									placeholder="最高金额" 
									shape="circle" 
									inputAlign="center"
									:customStyle="inputStyle"
									fontSize="13px"
								>
								</u-input>
							</view>
						</view>
						<view class="mt-20">
							<text class="fweigh-600">性别</text>
							<view class="flex flex--wrap mt-20">
								<view class="mr-20" v-for="(sex, sexIx) in sexs" :key="sex.value">
									<u-tag 
										:text="sex.label" 
										:name="sex.value" 
										:bgColor="sexIndex===sexIx?'#e54d42':'#fff'" 
										:borderColor="sexIndex===sexIx?'#e54d42':'#9a9a9a'" 
										:color="sexIndex===sexIx?'#fff':'#353535'" 
										:plain="sexIndex!==sexIx" 
										shape="circle" 
										size="large"
										@click="setData('sex', sex.value, sexIx)"
									/>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="flex popup-bottom-btn">
					<view class="width-200">
						<u-button type="primary" text="重置" color="#f3f3f3" :customStyle="{fontSize:'15px',color:'#2e2e2e'}" @click="resetting"/>
					</view>
					<view class="ml-20 width-320">
						<u-button type="primary" text="确定" color="#e54d42" :customStyle="{fontSize:'15px'}" @click="onSearch"/>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import listHeightMixin from '@/common/computedAltitude.js'
	import { mapState } from 'vuex'
	export default {
		mixins:[listHeightMixin],
		data() {
			return {
				showPopup: false,
				dataList:[],
				tabsList:[
					{name:'直推客户', value:0},
					{name:'间推客户', value:1}
				],
				currentTabs: 0,
				total: 0,
				timesIx:100,
				consumeIndex: 100,
				sexIndex: 100,
				loadMore: false,
				arriveDays:[
					{ label:'30天前',value:30 },
					{ label:'60天前',value:60 },
					{ label:'90天前',value:90 },
				],
				consumeTimes:[
					{ label:'仅1次',value:1 },
					{ label:'3次内',value:3 },
					{ label:'5次内',value:5 },
				],
				sexs:[
					{ label:'男',value:1 },
					{ label:'女',value:2 },
					{ label:'未知',value:3 },
				],
				searchData:{
					name:'',
					arriveDays:'',
					minCconsumeTime:'',
					maxCconsumeTime:'',
					minConsumeAmount:'',
					maxConsumeAmount:'',
					sex:'',
					page: 1,
					limit: 40
				},
				inputStyle:{
					borderColor:'#9a9a9a !important',
				}
			}
		},
		onLoad() {
			let that = this
			uni.$on('upUserData', val=>{
				that.onSearch()
			})
		},
		onShow() {
			if(this.userInfo.roleCategoryId===2){
				this.searchData.inviteStaffId = this.userInfo.userId
				this.searchData.listType = 0
			}else {
				this.searchData.inviteStaffId = 0
				this.searchData.listType = 0
			}
			this.onSearch()
		},
		computed: {
			...mapState(['userInfo'])
		},
		onUnload() {
			// 移除监听事件
			uni.$off('upUserData')
		},
		onReachBottom() {
			console.log('sdas', this.loadMore)
			if(this.loadMore){
				this.getPageList()
			}
		},
		methods: {
			onClickSwitchType(e){
				if(this.currentTabs === e.index) return
				this.currentTabs = e.index
				this.searchData.listType = e.value
				this.onSearch()
			},
			onSearch(){
				this.dataList = []
				this.searchData.page = 1
				this.getPageList()
			},
			resetting(){
				this.searchData.arriveDays=''
				this.searchData.minCconsumeTime=''
				this.searchData.maxCconsumeTime=''
				this.searchData.minConsumeAmount=''
				this.searchData.maxConsumeAmount=''
				this.searchData.sex=''
				this.timesIx = 100
				this.sexIndex = 100
				this.consumeIndex = 100
			},
			onSetTime(val){
				this.timesIx = 100
				this.searchData.arriveDays = val
			},
			setData(name, val, ix){
				switch(name){
					case 'arriveDays':
						this.timesIx = ix
						this.searchData.arriveDays = val
						break
					case 'consumeTime':
						this.consumeIndex = ix
						this.searchData.minCconsumeTime = 1
						this.searchData.maxCconsumeTime = val
						break
					case 'sex':
						this.sexIndex = ix
						this.searchData.sex = val
						break
				}
				
			},
			phoneCall(num){
				uni.makePhoneCall({ phoneNumber: num })
			},
			getPageList(){
				this.$reqPost('GetShareholderUsersPageList', {...this.searchData, clientType: this.userInfo.roleCategoryId}).then(res=>{
					this.total = res.otherData.userExpand.userCount
					this.dataList = [...this.dataList, ...res.data]
					if (res.count > this.dataList.length) {
						this.searchData.page++
						this.loadMore = true
					}else {
						this.loadMore = false
					}
				})
			},
		}	
	}
</script>

<style lang="scss" scoped>
	.ad{
		max-width: 100%;
		width:375rpx;
		height: 130rpx;
		border-radius: 10rpx;
	}
	.popup-bottom-btn{
		width: 540rpx;
		position:absolute;
		bottom: 80rpx;
		right: 20rpx;
	}
</style>
